package com.example.demo.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: H_tao_X
 * Date: 2022-08-01
 * Time: 20:39
 */

/**
 * 统一异常类
 */

//@ControllerAdvice
@RestControllerAdvice // 当前类是针对 controller 的通知类(增强类)
public class MyExceptionAdvice {


    @ExceptionHandler(ArithmeticException.class)
    public HashMap<String,Object> arithmeticExceptionAdvice(ArithmeticException e){
        HashMap<String,Object> result = new HashMap<>();
        result.put("state",-1);
        result.put("data",-1);
        result.put("msg","算术异常" + e.getMessage());
        return result;
    }

    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> exceptionAdvice(Exception  e){
        HashMap<String,Object> result = new HashMap<>();
        result.put("state",-1);
        result.put("data",-1);
        result.put("msg","算术异常" + e.getMessage());
        return result;
    }
}
